﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Contracts.Library
{
    public class Synchronizer<T>
    { 
        private readonly SynchronizationContext ctx = SynchronizationContext.Current; 
     
        public void Process(T msg) 
        { 
            if (this.ctx != null)
                this.ctx.Send(x => this.OnCompleted(msg), null); 
            else
                this.OnCompleted(msg); 
        }

        public void Process()
        {
            if (this.ctx != null)
                this.ctx.Send(x => this.OnComplete(), null);
            else
                this.OnComplete();
        }

        public event Action OnComplete; 

        public event Action<T> OnCompleted; 
    }  
}
